package cn.xorange.commons.exception;

import cn.xorange.commons.datasource.constants.DataErrorCode;
import cn.xorange.commons.utils.exception.BaseException;

import java.util.Objects;

/**
 * @author : yangjian
 * @date : 2023/8/23
 */
public class DataException extends BaseException {

    DataException(DataErrorCode errorCode){
        super(errorCode.getStatus(), errorCode.getCode(),errorCode.getMsg());
    }

    DataException(DataErrorCode errorCode, Throwable throwable){
        super(errorCode.getStatus(), errorCode.getCode(),errorCode.getMsg(),throwable);
    }


    public static DataException mybatisException(String msg){
        DataErrorCode code = DataErrorCode.MYBATIS_EXCEPTION;
        code.setMsg(msg);
        return new DataException(code);
    }

    public static DataException mybatisException(Throwable throwable){
        return new DataException(DataErrorCode.MYBATIS_EXCEPTION, throwable);
    }


    /**
     * 数据库运行异常
     */
    public static DataException databaseRunException(Throwable throwable) {
        return new DataException(DataExceptionUtils. runException(throwable));
    }

    /**
     * 切换数据源异常
     */
    public static DataException dataSourceChangeException(){
        return new DataException(DataErrorCode.DATASOURCE_CHANGE_EXCEPTION);
    }

    /**
     * 数据源未找到
     * @param key
     */
    public static DataException dataSourceNotFound(String key){
        DataErrorCode datasourceNotfound = DataErrorCode.DATASOURCE_NOTFOUND;
        datasourceNotfound.setMsg(datasourceNotfound.getMsg() + ": " + key);
        return new DataException(datasourceNotfound);
    }


    /**
     * sql error
     */
    public static DataException sqlError(Throwable throwable){
        DataErrorCode code = DataErrorCode.SQL_ERROR;
        code.setMsg(throwable.getCause().getMessage());
        throw new DataException(code,throwable);
    }

    /**
     * delete sql no where
     */
    public static DataException sqlDeleteNoWhere(){
        return new DataException(DataErrorCode.SQL_DELETE_NO_WHERE);
    }

    /**
     * update sql no where
     */
    public static DataException sqlUpdateNoWhere(){
        return new DataException(DataErrorCode.SQL_UPDATE_NO_WHERE);
    }

    /**
     * update sql 没有值
     */
    public static DataException sqlUpdateNoValue(){
        return new DataException(DataErrorCode.SQL_UPDATE_NO_VALUE_ERROR);
    }

    /**
     * 实体类没有设置主键列
     */
    public static DataException entityKeyColNotFound(){
        return new DataException(DataErrorCode.ENTITY_KEY_NOT_FOUND);
    }

    /**
     * 列名错误
     */
    public static DataException entityFieldNameError(String colName){
        DataErrorCode code = DataErrorCode.ENTITY_FIELD_NAME_ERROR;
        code.setMsg(colName + code.getMsg());
        return new DataException(code);
    }

    /**
     * 实体类批量新增异常
     */
    public static DataException entityBatchAddException(String msg){
        DataErrorCode code = DataErrorCode.ENTITY_BATCH_ADD_ERROR;
        if(Objects.nonNull(msg)){
            code.setMsg(msg);
        }
        return new DataException(code);
    }

    /**
     * 实体类列名错误
     */
    public static DataException entityUpdateOldValueNull(){
        return new DataException(DataErrorCode.ENTITY_UPDATE_OLD_VALUE_NULL);
    }

    /**
     * 版本控制类没有updateTime属性
     */
    public static DataException entityNoVersionProperty(){
        return new DataException(DataErrorCode.ENTITY_NO_VERSION_PROPERTY);
    }

    /**
     * 关联实体类异常
     * @param msg
     */
    public static DataException entityRelaNull(String msg){
        DataErrorCode code = DataErrorCode.ENTITY_RELA_EXCEPTION;
        code.setMsg(msg);
        return new DataException(code);
    }



    /**
     * 强制更新
     * @param fixMsg
     */
    public static DataException entityNoVersionProperty(String fixMsg){
        DataErrorCode code = DataErrorCode.ENTITY_NO_VERSION_PROPERTY;
        code.setMsg(code.getMsg()+ ": " + fixMsg);
        return new DataException(code);
    }

    /**
     * 实体类更新操作数据无变化
     */
    public static DataException dataUpdateNoDataChange(){
        return new DataException(DataErrorCode.DATA_UPDATE_NO_CHANGE);
    }

    /**
     * param 不符合规范，防sql注入风险
     */
    public static DataException dataViolationException(){
        return new DataException(DataErrorCode.DATA_VIOLATION_EXCEPTION);
    }


    /**
     * param 版本控制，数据已被修改
     */
    public static DataException dataVersionUpdateOld(){
        return new DataException(DataErrorCode.DATA_UPDATE_VERSION_OLD);
    }

    /**
     * 序列新增失败
     */
    public static DataException dataKeyBuildFail(){
        return new DataException(DataErrorCode.DATA_KEY_BUILD_FAIL);
    }

    /**
     * 序列为空
     */
    public static DataException dataKeyValueNull(){
        return new DataException(DataErrorCode.DATA_KEY_VALUE_NULL);
    }

    /**
     * 缺少必填参数
     */
    public static DataException dataValueNull(String msg){
        DataErrorCode code = DataErrorCode.DATA_VALUE_NULL;
        code.setMsg(msg + code.getMsg());
        return new DataException(code);
    }

    /**
     * 数据库 唯一性约束
     */
    public static DataException duplicateKeyException(Throwable throwable){
        DataErrorCode code = DataErrorCode.DUPLICATE_KEY_EXCEPTION;
        return new DataException(code,throwable);
    }

    /**
     * 字段长度限制错误
     */
    public static DataException dataLengthError(){
        return new DataException(DataErrorCode.DATA_LENGTH_ERROR);
    }


    /**
     * 字段重复限制错误
     */
    public static DataException dataRepeatError(String repeatName){
        DataErrorCode code = DataErrorCode.DATA_REPEAT;
        code.setMsg(code.getMsg() + ": " + repeatName );
        return new DataException(code);
    }

    /**
     * 重复列未标明重复条件
     */
    public static DataException dataRepeatAnnotationError(){
        return new DataException(DataErrorCode.DATA_REPEAT_ANNOTATION_ERROR);
    }

    /**
     * 树类型数据错误
     */
    public static DataException dataTreeException(String errMsg){
        DataErrorCode code = DataErrorCode.DATA_TREE_EXCEPTION;
        code.setMsg(errMsg);
        return new DataException(code);
    }

    /**
     * 数据绑定错误
     */
    public static DataException dataBandException(String errMsg){
        DataErrorCode code = DataErrorCode.DATA_BAND_EXCEPTION;
        code.setMsg(code.getMsg() + ": " + errMsg);
        return new DataException(code);
    }

    public static DataException dataColumnNullException(){
        return new DataException(DataErrorCode.DATA_COLUMN_NULL);
    }

    public static DataException paramException(String msg){
        DataErrorCode code = DataErrorCode.DATA_PARAMS_ERROR;
        code.setMsg(msg);
        return new DataException(code);
    }

}
